<template>
  <div class="app-container">
    <!-- 表头工具栏 -->
    <div class="toolbar-container">
      <el-input style="width: 200px;" v-model="searchTerm" />

      <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button style="margin-left: 10px;" type="primary" 
        icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button style="margin-left: 10px;" type="primary" 
        icon="el-icon-edit" @click="handleAddBat()">批量删除</el-button>        
    </div>

    <el-table v-loading="listLoading" :data="list" style="width: 100%">
      <!-- label: 表头文字、prop: 对应json数据里面的属性-->
      <el-table-column label="收派标准名称" prop="name" width="180">
      </el-table-column>

      <el-table-column label="最小重量" prop="minWeight" width="100">
      </el-table-column>
      <el-table-column label="最大重量" prop="maxWeight" width="100">
      </el-table-column>
      <el-table-column label="最小长度" prop="minLength" width="100">
      </el-table-column>
      <el-table-column label="最大长度" prop="maxLength" width="100">
      </el-table-column>

      <el-table-column label="操作时间" prop="operatingTime">
        <template slot-scope="scope">
          <span>{{ scope.row.operateTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination :current-page="pageQuery.page" 
        :page-sizes="[10,20,30, 50]" 
        :page-size="pageQuery.size" 
        :total="total" background 
        layout="total, sizes, prev, pager, next, jumper" 
        @size-change="onPageSizeChange" 
        @current-change="onPageChange"/>
    </div>

    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="item" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="名称">
          <el-input v-model="item.name" prop="name"/>
        </el-form-item>
        <el-form-item label="最小重量">
          <el-input v-model="item.minWeight" prop="minWeight"/>
        </el-form-item>
        <el-form-item label="最小长度">
          <el-input :autosize="{ minRows: 2, maxRows: 4}" prop="minLength"
            v-model="item.minLength" placeholder="请输入"/>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
        <el-button v-else type="primary" @click="updateData">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { fetchStanList, fetchPage, delSta, updateSta } from '@/api/standard'
export default {
  data() {
    return {
        list:[],
        listLoading:true,
        pageQuery: {
          page: 1,
          size: 20
        },
        total:null,
      item: {
        id: undefined,
        minLength: '233',
        name: '物流管理',
        minWeight: '111'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
        name: [{ required: true, message: '名称必须输入', trigger: 'change' }],
        // minLength: [{ required: true, message: '请输入是否分级', trigger: 'blur' }]
      },      
    }      
  },
    created() {
      // 页面创建的时候，去加载文章列表
      this.getList()
    },

  methods: {
    //批量删除
    handleAddBat(){

    },
    //添加
    handleCreate(){
      this.item={}
      this.dialogStatus='create'
      this.dialogFormVisible=true

    },                    
    //搜索
    handleSearch(){

    },
// 加载文章列表
    getList() {
        this.listLoading = true

      // 发送http请求，获取数据
      fetchPage(this.pageQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
       })
     },
    handleUpdate(row) {
      this.item = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },

    updateData() {
      updateSta(this.item).then(response=>{
        this.getList()
        this.dialogFormVisible=false
        this.$notify({
          title: '提示',
          message: response.data.message,
          duration: '2000'
        });         
      })
    },  

    handleDelete(row) {
      this.item=Object.assign({},row)
      delSta(this.item).then(response=>{
        this.getList()
        this.$notify({
          title: '提示',
          message: response.data.message,
          duration: '2000'
        });         
      })
    },

    onPageSizeChange(size) {
        this.pageQuery.size = size
        this.getList()
      },

      onPageChange(page) {
        this.pageQuery.page = page
        this.getList()
      }      

    }
   }
</script>